package com.zhgxi.zf.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slave4j.orm.PageData;
import org.slave4j.utils.WebUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;

import com.zhgxi.zf.entity.Menu;
import com.zhgxi.zf.entity.Role;
import com.zhgxi.zf.entity.RoleMenu;
import com.zhgxi.zf.service.RoleMenuService;

@Controller("RoleMenuAction")
public class RoleMenuAction {
	@Resource
	private RoleMenuService roleMenuService;

	public List<Menu> findMenuByRole(Role role) {
		return roleMenuService.findMenuByRole(role);
	}

	public boolean addRoleMenu(List<Menu> listMenu,Role role) {
		return roleMenuService.addRoleMenu(listMenu, role);
	}
	
	public ModelMap list(HttpServletRequest request) {
		PageData<RoleMenu> pageData = new PageData<RoleMenu>();
		// 给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		pageData = roleMenuService.find(pageData);

		return new ModelMap(pageData);
	}

	public String addForm(Model model) {
		return "/zf/roleMenu/input";
	}

	public String editForm(@PathVariable("id") Integer id, Model model) {
		RoleMenu roleMenu = roleMenuService.find(id);
		model.addAttribute(roleMenu);
		return "/zf/roleMenu/input";
	}

	public String save(RoleMenu roleMenu) {
		if (roleMenu.isNew()) {
			roleMenuService.save(roleMenu);
		} else {
			roleMenuService.update(roleMenu);
		}

		return "redirect:/zf/roleMenu/list";
	}

	public String delete(@PathVariable("id") Integer id) {
		roleMenuService.delete(id);
		return "redirect:/zf/roleMenu/list";
	}

	public String visible(@PathVariable("id") Integer id) {
		roleMenuService.visible(id);
		return "redirect:/zf/roleMenu/list";
	}

	public String unVisible(@PathVariable("id") Integer id) {
		roleMenuService.unVisible(id);
		return "redirect:/zf/roleMenu/list";
	}

	@InitBinder
	public void InitBinder(WebDataBinder dataBinder) {
		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
	}

}
